﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using BTM_Client_Application.SupportClasses;
using Client_Server_Interface;
using Xceed.Wpf.Toolkit;

namespace BTM_Client_Application.RealTimeFilter
{
    /// <summary>
    /// Логика взаимодействия для DateTimeFilterElementControl.xaml
    /// </summary>
    public partial class DateTimeFilterElementControl : FilterElementControl.IFilterElementControl
    {
        private readonly VoidDelegate _closeWindow;
        private readonly FilterElementType _filterType;

        public DateTimeFilterElementControl(FilterElementType type, VoidDelegate onFilterUpdated, VoidDelegate onClose)
        {
            InitializeComponent();
            
            Style = FindResource("MyPopup") as Style;
            _filterType = type;

            StartTimeGrid.Visibility = Visibility.Collapsed;

            EndTimeGrid.Visibility = Visibility.Collapsed;

            switch (type)
            {
                case FilterElementType.StartTime:
                    ShowStartPicker();
                    break;
                case FilterElementType.EndTime:
                    ShowEndPicker();
                    break;
                case FilterElementType.StartAndEndTime:
                    ShowStartPicker();
                    ShowEndPicker();
                    break;
                default:
                    throw new ArgumentOutOfRangeException("type");
            }
            /*
            StartTimePicker.CultureInfo = new CultureInfo("en-US");
            StartTimePicker.Format = DateTimeFormat.Custom;
            StartTimePicker.FormatString = "H:mm d/M/yyy";
            */

            /*
            EndTimePicker.CultureInfo = new CultureInfo("en-US");
            EndTimePicker.Format = DateTimeFormat.Custom;
            EndTimePicker.FormatString = "H:mm d/M/yyy";
             */
            _closeWindow = onClose;
            OnFilterUpdated = onFilterUpdated;
            Clear();
            UiLanguageChanged();
        }

        public void SetFilterActiveValues(FilterElementType valuesType, List<object> values)
        {
            switch (valuesType)
            {
                case FilterElementType.StartTime:
                    StartTimePicker.Value = (DateTime)values[0];
                    break;
                case FilterElementType.EndTime:
                    EndTimePicker.Value = (DateTime)values[0];
                    break;
                case FilterElementType.StartAndEndTime:
                    StartTimePicker.Value = (DateTime)values[0];
                    EndTimePicker.Value = (DateTime)values[1];
                    break;
                default:
                    throw new ArgumentOutOfRangeException("valuesType");
            }
        }

        public void SetFilterItems(List<object> filterElemets)
        {
            
        }

        public bool CheckElement(object elementToCheck)
        {
            //Заимплементить
            DateTime dt;
            if (!DateTime.TryParse(elementToCheck as String, out dt))
            {
                dt = (DateTime)elementToCheck;
            }
            
            switch (_filterType)
            {
                case FilterElementType.StartTime:
                    if (dt > StartTimePicker.Value)
                        return true;
                    break;
                case FilterElementType.EndTime:
                    if (dt < EndTimePicker.Value)
                        return true;
                    break;
                case FilterElementType.StartAndEndTime:
                    if (dt < EndTimePicker.Value && dt > StartTimePicker.Value)
                        return true;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            return false;
        }

        public void Clear()
        {
            StartTimePicker.ValueChanged    -= StartTimePicker_OnValueChanged;
            EndTimePicker.ValueChanged      -= EndTimePicker_OnValueChanged;

            StartTimePicker.Value = DateTime.Today;
            var setLast = DateTime.Today.AddDays(1);
            EndTimePicker.Value = setLast;

            StartTimePicker.ValueChanged += StartTimePicker_OnValueChanged;
        }

        public UIElement GetElement()
        {
            return this;
        }

        public VoidDelegate OnFilterUpdated { get; set; }

        public List<object> ForRequestString { get
        {
            var toRet = new List<object>();

            switch (_filterType)
            {
                case FilterElementType.StartTime:
                    if (StartTimePicker.Value == null)
                        throw new IncorrectInputValueException("StartTime", "NullValue");
                    toRet.Add((DateTime)StartTimePicker.Value);
                    break;
                case FilterElementType.EndTime:
                    if (EndTimePicker.Value == null)
                        throw new IncorrectInputValueException("EndTime", "NullValue");
                    toRet.Add((DateTime)EndTimePicker.Value);
                    break;
                case FilterElementType.StartAndEndTime:
                    if (StartTimePicker.Value == null)
                        throw new IncorrectInputValueException("StartTime", "NullValue");
                    if (EndTimePicker.Value == null)
                        throw new IncorrectInputValueException("EndTime", "NullValue");
                    toRet.Add((DateTime)StartTimePicker.Value);
                    toRet.Add((DateTime)EndTimePicker.Value);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            return toRet;
        }}

        private void ShowStartPicker()
        {
            StartTimePicker.CultureInfo = new CultureInfo("en-US");
            StartTimePicker.Format = DateTimeFormat.Custom;
            StartTimePicker.FormatString = "H:mm d/M/yyy";
            StartTimeGrid.Visibility = Visibility.Visible;
        }

        private void ShowEndPicker()
        {
            EndTimePicker.CultureInfo = new CultureInfo("en-US");
            EndTimePicker.Format = DateTimeFormat.Custom;
            EndTimePicker.FormatString = "H:mm d/M/yyy";
            EndTimeGrid.Visibility = Visibility.Visible;
        }

        private void CloseButton_OnClick(object sender, RoutedEventArgs e)
        {
            _closeWindow();
        }

        public void UiLanguageChanged()
        {
            StartTimeLabel.Content = Res.Rm.GetString("StartTime", AliveInfo.CurrentCulture);
            EndTimeLabel.Content = Res.Rm.GetString("EndTime", AliveInfo.CurrentCulture);
        }

        private void StartTimePicker_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if(OnFilterUpdated != null)
                OnFilterUpdated();
        }

        private void EndTimePicker_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (OnFilterUpdated != null)
                OnFilterUpdated();
        }
    }
}
